package social.hub.impl.jersey.util;

import static social.hub.api.google.buzz.BuzzConstants.OAUTH_SCOPE;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.ws.rs.core.MediaType;

import social.hub.api.ApiConfig;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.ClientRequest;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.client.filter.ClientFilter;
import com.sun.jersey.api.client.filter.LoggingFilter;
import com.sun.jersey.api.json.JSONConfiguration;
import com.sun.jersey.oauth.client.OAuthClientFilter;
import com.sun.jersey.oauth.signature.OAuthParameters;
import com.sun.jersey.oauth.signature.OAuthSecrets;

public class ClientFactory {

	public static class ContentTypeFilter extends ClientFilter {

		@Override
		public ClientResponse handle(ClientRequest request) throws ClientHandlerException {

			ClientResponse response = getNext().handle(request);

			List<String> list = response.getHeaders().get("content-type");

			String contentType = list.get(0);

			if (contentType.equals("text/javascript; charset=UTF-8")) {
				response.getHeaders().put("content-type", Collections.singletonList(MediaType.APPLICATION_JSON));
			}

			if (contentType.equals("text/xml;charset=UTF-8")) {
				response.getHeaders().put("content-type", Collections.singletonList(MediaType.APPLICATION_XML));
			}

			return response;

		}

	}

	public static Client createClient() {

		ClientConfig config = new DefaultClientConfig();

		Map<String, Object> props = config.getProperties();
		props.put(ClientConfig.PROPERTY_FOLLOW_REDIRECTS, true);
		props.put(ClientConfig.PROPERTY_CONNECT_TIMEOUT, 0);
		props.put(ClientConfig.PROPERTY_READ_TIMEOUT, 0);

		Map<String, Boolean> features = config.getFeatures();
		features.put(ClientConfig.FEATURE_DISABLE_XML_SECURITY, true);
		features.put(ClientConfig.FEATURE_FORMATTED, true);
		features.put(ClientConfig.FEATURE_XMLROOTELEMENT_PROCESSING, false);
		features.put(JSONConfiguration.FEATURE_POJO_MAPPING, true);

		Client client = Client.create(config);

		client.addFilter(new LoggingFilter());
		client.addFilter(new ContentTypeFilter());

		return client;

	}

	public static Client createClient(ApiConfig config) {
		return createClient(config, null);
	}

	public static Client createClient(ApiConfig config, String scope) {

		Client client = createClient();

		OAuthParameters oauthParameters = new OAuthParameters().consumerKey(config.getApiKey()).token(
				config.getAccessKey());
		OAuthSecrets oauthSecrets = new OAuthSecrets().consumerSecret(config.getApiSecret()).tokenSecret(
				config.getAccessSecret());

		if (scope != null) {
			oauthParameters.put("scope", OAUTH_SCOPE);

		}

		client.addFilter(new OAuthClientFilter(client.getProviders(), oauthParameters, oauthSecrets, null, null, null,
				null));

		return client;

	}

}
